<?php
/**
 * Created by PhpStorm.
 * User: CYJiao
 * Date: 6/18/15
 * Time: 15:45
 */

?>

<link rel="stylesheet" href="<?php echo Yii::app()->request->baseUrl; ?>/css/plugins/editer/themes/default/default.css">
<link rel="stylesheet" href="<?php echo Yii::app()->request->baseUrl; ?>/css/plugins/editer/plugins/code/prettify.css">

<script src="<?php echo Yii::app()->request->baseUrl; ?>/css/plugins/editer/kindeditor.js"></script>
<script src="<?php echo Yii::app()->request->baseUrl; ?>/css/plugins/editer/lang/zh_CN.js"></script>
<script src="<?php echo Yii::app()->request->baseUrl; ?>/css/plugins/editer/plugins/code/prettify.js"></script>
<style>
    #update_content{
        overflow: auto;
        padding: 0;
        margin: 0;
    }
</style>

<script>
		KindEditor.ready(function(K) {
			var editor1 = K.create('textarea[name="content"]', {
				cssPath : '<?php echo Yii::app()->request->baseUrl; ?>/css/plugins/editer/plugins/code/prettify.css',
				uploadJson : '<?php echo CController::createUrl('community/uploadfile'); ?>',
				fileManagerJson : '<?php echo Yii::app()->request->baseUrl; ?>/css/plugins/editer/file_manager_json.php',
				allowFileManager : true,
				afterCreate : function() {
					var self = this;
					K.ctrl(document, 13, function() {
						self.sync();
						K('form[name=add_content]')[0].submit();
					});
					K.ctrl(self.edit.doc, 13, function() {
						self.sync();
						K('form[name=add_content]')[0].submit();
					});
				}
			});
			prettyPrint();
		});
		function toVaild(){
			if($('#title').val() == ''){
				alert('标题不能为空');
				return false;
			}
			if($("input[name='topics[]']:checked").length < 1){
				alert('必须选择一个话题');
				return false;
			}else if($("input[name='topics[]']:checked").length > 3){
				alert('最多选择3个话题');
				return false;
			}
			$.ajax({
	            type: 'POST',
	            url: '<?php echo CController::createUrl('community/update') ;?>',

	            data: {data:$('#add_content').serializeArray()},
	            success: function(data){
	                if(data != ''){parent.
	                	console.log(data);
	                    if(parent.$('#main-tab').tabs('exists', '文章详情')){
	                		parent.$('#main-tab').tabs('select', '文章详情');
	                		parent.$('#main-tab').tabs('getSelected').panel('refresh','<?php echo CController::createUrl('community/find')?>&article_id='+data.article_id);
	                	}else{
	                		parent.$('#main-tab').tabs('add',{
					            title:'文章详情',
					            content:'<iframe scrolling="auto" frameborder="0"  src="<?php echo CController::createUrl('community/find')?>&article_id='+data.article_id+'" style="width:100%;height:100%;"></iframe>',
					            closable:true
				        	});

				        	parent.$('#main-tab').tabs('getSelected').panel('refresh','<?php echo CController::createUrl('community/find')?>&article_id='+data.article_id+'&from=list');
	        				parent.$('#main-tab').tabs('getSelected').css('width', 'auto');
	                	}	
	                }else{
	                	alert('更新失败');
	                    
	                }
	            },
	            dataType: 'json'
	        });

	        return false;
		}


</script> 

<div title="修改文章" style="height:1000px;" id="update_content">
	<div class="easyui-panel"  data-options="border:false,fit:true" title="" style="height:1000px;">
		<div style="padding:10px 60px 20px 60px">
		    <form method="post" name="add_content" id="add_content" onsubmit="return toVaild()" action="#">
		    	<table cellpadding="5">
		    		<tr>
		    			<td>标题:</td>
		    			<td><input class="easyui-textbox" type="text" id ="title" name="title" value="<?php echo $rows['title']?>"></input>
		    				<input type="hidden" id ="article_id" name="article_id" value="<?php echo $rows['article_id']?>"></input></td>
		    		</tr>
		    		<tr>
		    			<?php
		    			$topic_arr = array('内科','外科','妇科','儿科','中医保健','营养美食','其他');
		    			$top_arr = array();
		    			$result = array();
		    			foreach ($rows['topics'] as $id =>$top){
							$top_arr = json_decode($top,true);
							$result[$top_arr['topicId']] = $top_arr['topicName'];

						}
						?>
		    			<td>话题:</td>
		    			<td style="padding-top:15px;">
		    				<?php foreach($topic_arr as $key => $val){
		    				?>
		    				<?php echo '<span style="display:inline-block;vertical-align: middle;">'.$val.'</span>' ;
		    					  if(isset($result[$key+1])){
		    					  	$check = 'checked="checked"';
		    					  }else{
		    					  	$check = '';
		    					  }
		    				?>：<input class="easyui-checkbox" name="topics[]" style="vertical-align: middle;" value="<?php echo $key+1?>" type="checkbox" <?php echo $check?> >&nbsp;&nbsp;

		    				<?php
		    				}?>
		    			</td>
		    		</tr>
		    		<tr>
		    			<td>内容:</td>
		    			<td><textarea name="content" style="width:900px;height:280px;">
		    				<?php echo htmlspecialchars($rows['content'])?>
		    			</textarea></td>
		    		</tr>
		    		<tr>
		    	</table>
		    	<div style="margin-left:900px;padding:5px">
		    	<input class="easyui-linkbutton" style="width:50px;height:50px;" type="submit" name="submit" value="提交" data-options="iconCls:'icon-save'"/>
		    	
		        </div>
		    </form>
		    
		</div>
	</div>
</div>
